package com.example.studentalarm.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.studentalarm.R;
import com.example.studentalarm.bean.TimetableBean;
import com.example.studentalarm.dialog.PickerDialog;
import com.example.studentalarm.presenter.EditTimetableMvpPresenter;
import com.example.studentalarm.utils.StringUtil;
import com.example.studentalarm.utils.TimeUtils;
import com.example.studentalarm.utils.Utils;
import com.example.studentalarm.view.IEditTimetableView;
import com.example.studentalarm.widget.ClearEditText;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/***
 * 新增、编辑课程表activity
 */
public class EditTimetableActivity extends BaseModelActivity<IEditTimetableView, EditTimetableMvpPresenter> implements IEditTimetableView {


    @BindView(R.id.tvGoBack)
    TextView tvGoBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvCommit)
    TextView tvCommit;
    @BindView(R.id.editTimetable)
    ClearEditText editTimetable;
    @BindView(R.id.tvDateTime)
    TextView tvDateTime;
    @BindView(R.id.editAddress)
    ClearEditText editAddress;
    @BindView(R.id.editRemark)
    ClearEditText editRemark;
    @BindView(R.id.tv_minute)
    TextView tvMinute;
    private TimetableBean timetableBean = new TimetableBean();
    private Calendar selectedDate, startDate, endDate;
    private PickerDialog dialog;

    @Override
    public int getLayoutUrl() {
        return R.layout.edit_timetable_layout;
    }

    @Override
    public void registerById() {
        ButterKnife.bind(this);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy ");
        String year_str = formatter_year.format(curDate);
        int year_int = (int) Double.parseDouble(year_str);

        SimpleDateFormat formatter_mouth = new SimpleDateFormat("MM ");
        String mouth_str = formatter_mouth.format(curDate);
        int mouth_int = (int) Double.parseDouble(mouth_str);

        SimpleDateFormat formatter_day = new SimpleDateFormat("dd ");
        String day_str = formatter_day.format(curDate);
        int day_int = (int) Double.parseDouble(day_str);

        selectedDate = Calendar.getInstance();//系统当前时间
        startDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        endDate = Calendar.getInstance();
        endDate.set(year_int + 50, mouth_int - 1, day_int);
    }

    @Override
    public void initData() {
        presenter.attach(this);
        dialog = new PickerDialog(this);
        dialog.setOnItemClick(new PickerDialog.ItemOnClick() {
            @Override
            public void onClick(String minute) {
                tvMinute.setText(minute);
            }
        });
        Integer id = getTimetableID();
        if (id != null) {
            timetableBean = presenter.getTimetableBean(id);
            tvTitle.setText(new StringBuilder(timetableBean.getName()).append("课程表").toString());
            tvMinute.setText(String.valueOf(timetableBean.getMinute()));
            tvCommit.setText("修改");
            editTimetable.setText(StringUtil.isEmpty(timetableBean.getName()) ? "" : timetableBean.getName());
            editAddress.setText(StringUtil.isEmpty(timetableBean.getAddress()) ? "" : timetableBean
                    .getAddress());
            editRemark.setText(StringUtil.isEmpty(timetableBean.getNote()) ? "" : timetableBean.getNote());
            tvDateTime.setText(timetableBean.getTime() == null ? "" : TimeUtils.getDate("yyyy-MM-dd HH:mm", timetableBean.getTime().getTime()));
        } else {
            tvTitle.setText("新增课程表");
            tvCommit.setText("新增");
        }
    }

    @Override
    protected EditTimetableMvpPresenter initPresenter() {
        return new EditTimetableMvpPresenter(this);
    }


    @OnClick({R.id.tvGoBack, R.id.tvCommit, R.id.tvDateTime, R.id.tv_minute})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvGoBack:
                finishActivity();
                break;
            case R.id.tv_minute:
                if (!dialog.isShowing()) {
                    dialog.show();
                }
                break;
            case R.id.tvDateTime:
                Utils.closeSoftKeyboard(this);
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        tvDateTime.setText(TimeUtils.getDate("yyyy-MM-dd HH:mm", date.getTime()));
                        timetableBean.setTime(date);
                        }
                        })
                        .setType(new boolean[]{true, true, true, true, true, false}) //年月日时分秒 的显示与否，不设置则默认全部显示
                        .setLabel("年", "月", "日", "时", "分", "")//默认设置为年月日时分秒
                        .isCenterLabel(false)
                        .setDividerColor(getResources().getColor(R.color.alert_color))
                        .setTextColorCenter(getResources().getColor(R.color.font_black)) // 设置选中项的颜色
                        .setTextColorOut(getResources().getColor(R.color.edit_font_black)) // 设置没有被选中项的颜色
                        .setContentSize(20)
                        .setDate(selectedDate)
                        .setLineSpacingMultiplier(1.2f)
                        .setRangDate(startDate, endDate)
                        .setDecorView(null)
                        .build();

                pvTime.setDate(Calendar.getInstance());
                pvTime.show();
                break;
            case R.id.tvCommit:
                timetableBean.setName(editTimetable.getText().toString().trim());
                timetableBean.setAddress(editAddress.getText().toString().trim());
                timetableBean.setNote(editRemark.getText().toString().trim());
                String minute = tvMinute.getText().toString().trim();
                if (!StringUtil.isEmpty(minute))
                timetableBean.setMinute(Integer.parseInt(minute));
                presenter.commitTimetable(timetableBean);
                if (tvCommit.getText().equals("新增")){
                    finishActivity();
                }
                break;

        }
    }


    @Override
    public Integer getTimetableID() {
        Bundle bundle = getIntent().getExtras();
        return bundle == null ? null : bundle.getInt("id");
    }


}
